package com.carb.controller.admin;

import com.carb.annotation.RequiredRoles;
import com.carb.constant.AuthorityConstant;
import com.carb.dto.MemberPageQueryDTO;
import com.carb.dto.MemberUpdateDTO;
import com.carb.dto.UserAddDTO;
import com.carb.result.PageResult;
import com.carb.result.Result;
import com.carb.service.IMemberService;
import com.carb.vo.MemberVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 会员管理 前端控制器
 * </p>
 *
 * @author XuChaoyang
 * @since 2025-01-18
 */
@RestController("adminMemberController")
@RequestMapping("/admin/member")
@Tag(name = "会员管理相关接口")
@Slf4j
public class MemberController {

    @Resource
    private IMemberService memberService;

    /**
     * 新增会员
     * @param userAddDTO 新增会员信息
     */
    @RequiredRoles({AuthorityConstant.ADMIN, AuthorityConstant.STORE_MANAGER})
    @PostMapping("/add")
    @Operation(summary = "新增会员")
    public Result<String> addEmployee(@RequestBody UserAddDTO userAddDTO) {
        log.info("新增会员：{}", userAddDTO);
        memberService.addMember(userAddDTO);
        log.info("新增会员成功！");
        return Result.success();
    }

    /**
     * 会员分页查询
     * @param memberPageQueryDTO 查询条件
     * @return 会员分页查询结果
     */
    @GetMapping("/page")
    @Operation(summary = "会员分页查询")
    public Result<PageResult<MemberVO>> pageQuery(MemberPageQueryDTO memberPageQueryDTO) {
        log.info("会员分页查询：{}", memberPageQueryDTO);
        PageResult<MemberVO> pageResult = memberService.pageQuery(memberPageQueryDTO);
        log.info("会员分页查询成功！");
        return Result.success(pageResult);
    }

    /**
     * 根据会员id查询会员信息
     * @param id 会员id
     * @return 查询结果
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据会员id查询会员信息")
    public Result<MemberVO> getMemberById(@PathVariable @Parameter(description = "会员id") Long id) {
        log.info("根据会员id查询会员信息，会员id：{}", id);
        MemberVO memberVO = memberService.getMemberById(id);
        log.info("根据会员id查询会员信息成功！");
        return Result.success(memberVO);
    }

    /**
     * 修改会员信息
     * @param memberUpdateDTO 修改的会员信息
     */
    @RequiredRoles({AuthorityConstant.ADMIN, AuthorityConstant.STORE_MANAGER})
    @PutMapping
    @Operation(summary = "修改会员信息")
    public Result<String> updateMember(@RequestBody MemberUpdateDTO memberUpdateDTO) {
        log.info("修改会员信息：{}", memberUpdateDTO);
        memberService.updateMember(memberUpdateDTO);
        log.info("修改会员信息成功！");
        return Result.success();
    }

    /**
     * 删除会员
     * @param id 会员id
     */
    @RequiredRoles({AuthorityConstant.ADMIN, AuthorityConstant.STORE_MANAGER})
    @DeleteMapping("/{id}")
    @Operation(summary = "删除会员")
    public Result<String> deleteMember(@PathVariable @Parameter(description = "会员id") Long id) {
        log.info("删除会员，会员id：{}", id);
        memberService.deleteMember(id);
        log.info("删除会员成功！");
        return Result.success();
    }

    /**
     * 会员余额充值
     * @param id 会员id
     * @param amount 充值金额（整数，单位为元）
     */
    @RequiredRoles({AuthorityConstant.ADMIN, AuthorityConstant.STORE_MANAGER, AuthorityConstant.CASHIER})
    @PostMapping("/recharge/{amount}")
    @Operation(summary = "会员余额充值")
    public Result<String> rechargeBalance(@PathVariable @Parameter(description = "充值金额") Integer amount,
                                          @Parameter(description = "会员id") Long id) {
        log.info("会员余额充值，会员id：{}，充值金额：{}", id, amount);
        memberService.rechargeBalance(id, amount);
        log.info("会员余额充值成功！");
        return Result.success();
    }

}
